package com.dangf.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dangf.entity.User;
import com.dangf.service.UserService;

/**
 * 用户管理 控制器
 * @author Hello_海生
 * @date 2014年3月27日
 */

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	
	//注册用户
	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> insert(User user){
		Map<String, Object> map = new HashMap<String, Object>();
		if(user.getEmail()!=null){
			User u = userService.finByEmail(user.getEmail());
			if(u==null){
				long id = (Long) userService.save(user);
				if(id!=0){
					u = userService.findById(id);
					map.put("message", "恭喜你，注册成功！");
					map.put("static","success");
					map.put("user",u);
				}else{
					map.put("message", "系统错误，注册失败！");
					map.put("static","error");
				}
			}else{
				map.put("message", "注册失败，此邮箱已存在。");
				map.put("static","error");
			}
		}else{
			map.put("message", "注册失败，请输入邮箱。");
			map.put("static","error");
		}
		return map;
	}
	
	//查询用户信息
	@RequestMapping("/show")
	@ResponseBody
	public Map<String, Object> show(User user){
		Map<String, Object> map = new HashMap<String, Object>();
		User u = null;
		if(user.getEmail()!=null){
			u = userService.finByEmail(user.getEmail());
		}else if(user.getId()!=0){
			u = userService.findById(user.getId());
			System.out.println("-----------"+u);
		}else{
			map.put("message", "查询失败，请输入 邮箱或 ID。");
			map.put("static","error");
		}
		
		if(u!=null){
			map.put("message", "信息查询成功！");
			map.put("static","success");
			map.put("user",u);
		}else{
			map.put("message", "查询失败，此ID 或 邮箱 不存在。");
			map.put("static","error");
		}
		
		return map;
	}
	
}
